<template>
  <div class="createdApiForm">
    <el-form ref="form" :model="form" label-width="80px" label-position="top">
      <!--api状态-->
      <apiState ref="apiStateDom" :form="form" @changeFormFun="changeFormFun"></apiState>
      <!--apiPath-->
      <apiPath ref="apiPathDom" :form="form" @changeFormFun="changeFormFun"></apiPath>
      <!--apiName-->
      <apiName ref="apiNameDom" :form="form" @changeFormFun="changeFormFun"></apiName>
      <!--api标签-->
      <apiTags ref="apiTagsDom" :form="form" @changeFormFun="changeFormFun"></apiTags>
      <!--负责人-->
      <apiPersonCharge ref="apiPersonChargeDom" :form="form" @changeFormFun="changeFormFun"></apiPersonCharge>
      <!--请求参数-->
      <requestParameter ref="requestParameterDom" :form="form" @changeFormFun="changeFormFun"></requestParameter>
      <!--响应内容-->
      <responseContent ref="responseContentDom" :form="form" @changeFormFun="changeFormFun"></responseContent>
    </el-form>
  </div>
</template>

<script>
  import apiState from './addApi/apiState'
  import apiPath from './addApi/apiPath'
  import apiName from './addApi/apiName'
  import apiTags from './addApi/apiTags'
  import apiPersonCharge from './addApi/apiPersonCharge'
  import requestParameter from './addApi/requestParameter'
  import responseContent from './addApi/apiResponse'
  export default {
    name: 'apiDescribe',
    data() {
      return {
        addApiData: {},
        form: {
          status: '', // api状态
          type: 'http', // api类型 - （没用到）
          protocol: '', // api协议
          method: '', // 请求类型
          uri: '', // 请求地址url
          name: '', // api名称
          groupId: '', // 分组id
          tags: '', // api标签
          manageUserName: '', // 负责人名称
          manageUserId: '', // 负责人id
          dataStructureIds: '', // 数据结构ID
          // ------------请求参数---------------
          reqParamType: '', // 请求参数类型（FormData、JSON、Raw、XML、Binary等）
          reqParamJsonType: '', // 请求参数JSON类型（object、array）
          reqParamRawType: '', // 请求参数RAW类型（json、xml、html、text等）
          reqHeaderInfo: [], // API 请求头部参数信息
          reqBodyInfo: [], // API 请求Body信息（reqParamType类型为FormData、JSON、XML时，赋值）
          reqParamRaw: '', // 请求参数RAW（reqParamType类型为Raw时，赋值）
          reqParamBinaryDesc: '', // 请求参数Binary描述（reqParamType类型为Binary时，赋值）
          urlParam: [], // query参数
          restfulParam: [], // REST参数
          authInfo: {
            type: '',
            content: {}
          }, // 权限校验
          // ------------请求参数---------------
          // ------------响应参数---------------
          respHeaderInfo: [], // API 响应头部参数信息
          respResultType: '', // 响应结果类型（FormData、JSON、Raw、XML、Binary等）
          respResultJsonType: '', // 响应结果JSON类型（object、array）
          respResultRawType: '', // 响应结果RAW类型（json、xml、html、text等）
          respBodyInfo: [], // API 响应Body信息（respResultType类型为JSON、XML时，赋值）
          respResultRaw: '', // 响应结果RAW（respResultType类型为Raw时，赋值）
          respResultBinaryDesc: '' // 响应结果Binary描述（respResultType类型为Binary时，赋值）
          // ------------响应参数---------------
        }
      }
    },
    components: {
      apiState,
      apiPath,
      apiName,
      apiTags,
      apiPersonCharge,
      requestParameter,
      responseContent
    },
    methods: {
      dataRemove(val) {
        const _this = this
        val.forEach(item => {
          _this.$delete(item, 'addRow')
          if (item.required === true) {
            _this.$set(item, 'required', 1)
          } else {
            _this.$set(item, 'required', 0)
          }
        })
        if (val.length > 1) {
          val.pop()
        }
        return val
      },
      copyDeep(templateData) {
        // templateData 是要复制的数组或对象，这样的数组或者对象就是指向新的地址的
        return JSON.parse(JSON.stringify(templateData))
      },
      getApiData() {
        const _this = this
        this.$refs.requestParameterDom.getDataAll()
        this.$refs.responseContentDom.getDataAll()
        this.form.reqParamType = this.$refs.requestParameterDom.$data.requestorRadio
        this.form.dataStructureIds = this.$refs.requestParameterDom.$data.dataStructureIds
        if (this.form.dataStructureIds.length > 0) {
          const dataStructureIds = this.form.dataStructureIds.concat(this.$refs.responseContentDom.$data.dataStructureIds)
          this.form.dataStructureIds = dataStructureIds
        } else {
          this.form.dataStructureIds = this.$refs.responseContentDom.$data.dataStructureIds
        }
        if (this.$refs.requestParameterDom.$data.tableData.length > 0) {
          console.log('qweqwe', this.dataRemove(this.copyDeep(this.$refs.requestParameterDom.$data.tableData)))
          this.form.reqHeaderInfo = this.dataRemove(this.copyDeep(this.$refs.requestParameterDom.$data.tableData))
        }
        switch (this.form.reqParamType) {
          case 'FormData':
            _this.form.reqBodyInfo = this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableFormData)
            break
          case 'JSON':
            _this.form.reqParamJsonType = this.$refs.requestParameterDom.$data.optionsJsonVal
            _this.form.reqBodyInfo = this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableJson)
            break
          case 'XML':
            console.log('XML1', this.dataRemove(this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableXML)))
            _this.form.reqBodyInfo = this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableXML)
            break
          case 'Raw':
            _this.form.reqParamRawType = _this.$refs.requestParameterDom.$data.reqParamRawTypeData
            _this.form.reqParamRaw = _this.$refs.requestParameterDom.getCodeMirrorData()
            break
          case 'Binary':
            _this.form.reqParamBinaryDesc = _this.$refs.requestParameterDom.$data.textareaBinary
            break
        }
        console.log('this.dataRemove', this.$refs.requestParameterDom.$data.requestBodyTableQuery)
        this.form.urlParam = this.dataRemove(this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableQuery))
        this.form.restfulParam = this.dataRemove(this.copyDeep(this.$refs.requestParameterDom.$data.requestBodyTableREST))
        this.form.authInfo.type = this.$refs.requestParameterDom.$data.AuthorityVerification
        if (this.$refs.requestParameterDom.$data.selectVal === '1') {
          this.form.authInfo.content = this.$refs.requestParameterDom.$data.basicAuth
        } else if (this.$refs.requestParameterDom.$data.selectVal === '2') {
          this.form.authInfo.content = this.$refs.requestParameterDom.$data.jewData
        }
        // 响应内容
        if (this.$refs.responseContentDom.$data.tableData.length > 0) {
          this.form.respHeaderInfo = this.dataRemove(this.copyDeep(this.$refs.responseContentDom.$data.tableData))
        }
        this.form.respResultType = this.$refs.responseContentDom.$data.requestorRadio
        switch (this.form.respResultType) {
          case 'JSON':
            _this.form.respResultJsonType = this.$refs.responseContentDom.$data.optionsJsonVal
            _this.form.respBodyInfo = this.copyDeep(this.$refs.responseContentDom.$data.requestBodyTableJson)
            break
          case 'XML':
            _this.form.respBodyInfo = this.copyDeep(this.$refs.responseContentDom.$data.requestBodyTableXML)
            break
          case 'Raw':
            _this.form.respResultRawType = _this.$refs.responseContentDom.$data.reqParamRawTypeData
            _this.form.respResultRaw = _this.$refs.responseContentDom.getCodeMirrorData()
            break
          case 'Binary':
            _this.form.respResultBinaryDesc = _this.$refs.responseContentDom.$data.textareaBinary
            break
        }
        console.log('this.formthis.formthis.formthis.form - ', this.form)
        return this.form
      },
      changeFormFun(data) {
        this.form = data
        console.log(this.form)
      }
    }
  }
</script>

<style scoped>

</style>
